"use client";

import { createContext, useEffect, useState } from "react";

export const ProfileContext = createContext({});

const DEFAULT_PROFILE = [
  {
    profileId: 1,
    profileName: "板载配置",
  }
]

function ProfileProvider({ children }: {children: any}) {
  const [profileItems, setProfileItems] = useState();
  const [currentProfileItem, setCurrentProfileItem] = useState();

  useEffect(() => {
    const localProfiles = localStorage.getItem('profiles');
    if (localProfiles && localProfiles !== 'undefined') {
      const localProfilesArr = JSON.parse(localProfiles);
      if (Array.isArray(localProfilesArr)) {
      }
    } else {
    }
    console.log(profileItems, 'profileItems')
  }, []);

  useEffect(() => {
    if(!profileItems) return;
    setCurrentProfileItem(profileItems[0]);
    console.log(profileItems, 'profileItems')
    localStorage.setItem('profiles', JSON.stringify(profileItems));
  }, [profileItems]);

  const profileProps = {
    profileItems: profileItems,
    setProfileItems: setProfileItems,
    currentProfileItem: currentProfileItem,
    setCurrentProfileItem: setCurrentProfileItem,
    editor: {},
  };
  return (
    <ProfileContext.Provider value={profileProps}>
      {children}
    </ProfileContext.Provider>
  );
}

export default ProfileProvider;
